package com.trf.member.base;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @author 小小唐
 * @Date 2023/3/3-星期五-23:35
 * Result类是为了统一规范响应JSON的格式
 * @Data 生成setter、getter、toString、hashCode、equals
 * 和 @RequiredArgsConstructor 实现方法
 */
@Data
public class Result implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(Result.class);

    private static final long serialVersionUID = 1L;

    /**
     * 响应业务状态码
     */
    private Integer code;

    /**
     * 是否正常
     */
    private Boolean flag;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应中的数据
     */
    private Object data;

    public Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
        //
        this.flag = code == ResultEnum.SUCCESS.getCode() ? true :false;
    }

    public static Result ok(){
        return new Result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(),null);
    }

    public static Result ok(Object data){
        return new Result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(), data);
    }

    public static Result ok(String message,Object data){
        return new Result(ResultEnum.SUCCESS.getCode(), message,data);
    }

    public static Result error(String message){
        logger.debug("返回错误：code={},message={}",ResultEnum.ERROR.getCode(),message);
        return new Result(ResultEnum.ERROR.getCode(), message,null);
    }

    public static Result build(int code,String message){
        logger.debug("返回结果：code={},message={}",code,message);
        return new Result(code,message,null);
    }

    public static Result build(ResultEnum resultEnum){
        logger.debug("返回结果：code={},message={}",resultEnum.getCode(),resultEnum.getDesc());
        return new Result(resultEnum.getCode(),resultEnum.getDesc(),null);
    }

    public String toString(){
        return JSON.toJSONString(this);
    }
}
